package org.eddm.ui.frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import org.flexdock.demos.perspective.PerspectivesDemo;
import org.flexdock.demos.util.DemoUtility;
import org.flexdock.demos.util.VSNetStartPage;
import org.flexdock.docking.DockableFactory;
import org.flexdock.docking.DockingConstants;
import org.flexdock.docking.DockingManager;
import org.flexdock.docking.state.PersistenceException;
import org.flexdock.perspective.LayoutSequence;
import org.flexdock.perspective.Perspective;
import org.flexdock.perspective.PerspectiveFactory;
import org.flexdock.perspective.PerspectiveManager;
import org.flexdock.perspective.actions.OpenPerspectiveAction;
import org.flexdock.perspective.persist.FilePersistenceHandler;
import org.flexdock.perspective.persist.PersistenceHandler;
import org.flexdock.util.ResourceManager;
import org.flexdock.util.SwingUtility;
import org.flexdock.view.View;
import org.flexdock.view.Viewport;
import org.flexdock.view.actions.DefaultDisplayAction;
import org.eddm.ui.panel.*;

/**
 * @author Andre Luiz Reis
 */
public class ApplicationFrame extends JFrame implements DockingConstants{
	
	public static final String PERSPECTIVE_FILE = "PerspectiveDemo5.data";	
	private static final String EXPLORER_VIEW = "bird.view";
	private static final String DATASTORE_VIEW = "message.log";
	private static final String MAP_VIEW = "problem";
	private static final String CONSOLE_VIEW = "console";
				
	public ApplicationFrame() {
		super("EDDM");		
		
		setContentPane(createContentPane());
		setJMenuBar(createApplicationMenuBar());
	}	

	private JPanel createContentPane() {
		JPanel contentPane = new JPanel(new BorderLayout());
		contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

		Viewport viewport = new Viewport();
		contentPane.add(viewport, BorderLayout.CENTER);
		
		View mainPage = createMainPage();
		
		LibraryPanel libPanel = new LibraryPanel();
		View view1 = libPanel.createView("solution.explorer", "Library");
		
		ProblemsPanel probPanel = new ProblemsPanel();
		View view2 = probPanel.createView("task.list", "Problems");
				
		viewport.dock(mainPage);
		mainPage.dock(view1, WEST_REGION, .2f);
		mainPage.dock(view2, SOUTH_REGION, .7f);
				
		return contentPane;
	}
	
	private View createMainPage() {
		String id = "mainPage";
		View view = new View(id, null, null);
		view.setTerritoryBlocked(CENTER_REGION, true);
		view.setTitlebar(null);
		view.setContentPane(new  ExplorerPanel());
		return view;
	}
	
	private View createView(String id, String text) {
		View view = new View(id, text);
		//view.addAction(CLOSE_ACTION);
		view.addAction(PIN_ACTION);

		JPanel p = new JPanel();
		//p.setBackground(Color.WHITE);
		p.setBorder(new LineBorder(Color.GRAY, 1));

		JTextField t = new JTextField(text);
		t.setPreferredSize(new Dimension(100, 20));
		p.add(t);

		view.setContentPane(p);
		return view;
	}
	
	private JMenuBar createApplicationMenuBar() {
		JMenuBar menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		JMenu windowMenu = new JMenu("Window");
		JMenu helpMenu = new JMenu("Help");
		
		JMenu showViewMenu = new JMenu("Show View");

		showViewMenu.add(new DefaultDisplayAction(EXPLORER_VIEW));
		showViewMenu.add(new DefaultDisplayAction(DATASTORE_VIEW));
		showViewMenu.add(new DefaultDisplayAction(MAP_VIEW));
		showViewMenu.add(new DefaultDisplayAction(CONSOLE_VIEW));
		
		windowMenu.add(showViewMenu);
		
		menuBar.add(fileMenu);
		menuBar.add(windowMenu);
		menuBar.add(helpMenu);
		
		return menuBar;
	}
	
	
	
}
